package com.ymatou.iapi.productmanager.testcase;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.service.RemoveProductFromLiveCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.productdata.impl.ProductFactory;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.mongo.jproduct.LiveProductsWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;


public class Ts_RemoveProductFromLive {	
	private static RemoveProductFromLiveCall removeProductFromLiveCall = new RemoveProductFromLiveCall();
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	private static IntergratedProductMapper intergratedProductMapper = SqlMapperFactory.getIntergratedProductMapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商品移出直播");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(removeProductFromLiveCall.getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	

	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveProductFromLive_001() {
		Logger.start(true,"商品移出直播 ");
		try {
			
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			List<String> productIds = ProductFactory.getProductWithExistStartLive(sellerId, sellerName, liveId, 3)
												.stream().map(x->x.getProductId()).collect(Collectors.toList());
			
			//移出直播
			JSONObject response = removeProductFromLiveCall.callAndGetReturnData(MapUtil.hashMap("ProductId", productIds.get(0),"LiveId", liveId,"UserId", sellerId));
			
			checkRemoveProductFromLive(response,liveId,Arrays.asList(productIds.get(0)),2,2);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	
	
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RemoveProductFromLive_002() {
		Logger.start(true,"不是当前买手商品 移出直播 ");
		try {
			
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			List<String> productIds = ProductFactory.getProductWithExistStartLive(sellerId, sellerName, liveId, 3)
												.stream().map(x->x.getProductId()).collect(Collectors.toList());
			
			//移出直播
			JSONObject response = removeProductFromLiveCall.callAndGetReturnData(MapUtil.hashMap("ProductId", productIds.get(0),"LiveId", liveId,"UserId", sellerId+1));
			Logger.verifyEquals(200, response.getInt("Code"), "返回码检查");
			
			List<Map<String,Object>> liveProdInSql = intergratedProductMapper.selectLiveProdLiveId(liveId);
			int prodCount = liveProdInSql.stream().filter(x->((Integer)x.get("iAction"))==0).collect(Collectors.toList()).size();
			int putAwayCount = liveProdInSql.stream().filter(x->((Integer)x.get("iAction"))==0&&((Integer)x.get("iSellStatus"))==1).collect(Collectors.toList()).size();
			
			Logger.verifyEquals(3, prodCount, "productInLive product count");
			Logger.verifyEquals(3,putAwayCount, "productInLive put away count");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	
	
	

	
	
	
	
	
	
	public static void checkRemoveProductFromLive(JSONObject ret, int liveId,List<String> prodIdList,int tgtProdCount,int tgtputAwayCount) throws Exception{
		Logger.verifyEquals(200, ret.getInt("Code"), "返回码检查");
		Logger.verifyEquals(false, ret.isNull("Data"), "正常移出不返回 null");
	/*	List<Map<String,Object>> liveProdInSql = intergratedProductMapper.selectLiveProdLiveId(liveId);
		int prodCount = liveProdInSql.stream().filter(x->((Integer)x.get("iAction"))==0).collect(Collectors.toList()).size();
		int putAwayCount = liveProdInSql.stream().filter(x->((Integer)x.get("iAction"))==0&&((Integer)x.get("iSellStatus"))==1).collect(Collectors.toList()).size();
		
		Logger.verifyEquals(tgtProdCount, prodCount, "productInLive product count");
		Logger.verifyEquals(tgtputAwayCount,putAwayCount, "productInLive put away count");*/
		
		
		for (String prod:prodIdList ){
			List<Map<String,Object>> retInSql = intergratedProductMapper.selectLiveProdByProdIdAndLiveId(prod, liveId);
			Logger.verifyEquals(1, retInSql.size(), "移除直播后，SQL商品直播Action 为-1");
			Logger.verifyEquals(-1, (Integer)retInSql.get(0).get("iAction"), "移除直播后，SQL商品直播Action 为-1");
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(prod);
				Logger.verifyEquals(0,new LiveProductsWapper().getCountByProductId(prod),"Mongo-LiveProducts 移出直播后，直播商品记录删除");
			}
		}
		
		if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
			NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(liveId);
		}
		
	}
	
	

}













